/**
 * Created by songj on 2015/12/11.
 */
define(['angular',
    'framework/msg/GillionMsgModule',
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/datasource/DataSourceModule',
    'framework/dropdown/GillionDropdownModule',
    'framework/date/DateModule',
    'framework/date/DateModule',
    'components/DateRangerModule',
    'angular-underscore'
], function (angular) {
    var module = angular.module('ReportModule',
        ['GillionMsgModule', 'DataGridModule', 'GillionPaginationModule',
            'DataSourceModule', 'DateModule', 'GillionDropdownModule','DateRangerModule'])
        .controller('ReportController', ['$scope', 'GillionMsgService', '$dataSourceManager', '$timeout', 'Arrays', "$location",
                function ($scope, GillionMsgService, $dataSourceManager, $timeout, Arrays, $location) {
                    $scope.params = {};
                    $scope.search = function () {
                        getDataRange($scope);
                        $dataSourceManager.dataSources["reportSource"].params = $scope.params;
                        $dataSourceManager.dataSources["reportSource"].doRequestData();
                    };
                    $scope.$on("reportSource",function(context,dataSource){
                        console.log(dataSource.records);
                        var totalRecord  = dataSource.records;

                    });
                    function getDataRange($scope) {
                        if ($scope.dateRange.rangeType === "ONE_DAY") {
                            var endTime = new Date(),
                                beginTime = new Date();
                            endTime.setDate(endTime.getDate());
                            beginTime.setDate(beginTime.getDate()-1);
                            beginTime = beginTime.toISOString();
                            $scope.params.startTime = beginTime;
                            $scope.params.endTime = endTime.toISOString();
                        } else if ($scope.dateRange.rangeType === "ONE_WEEK") {
                            var endTime = (new Date()).toISOString(),
                                beginTime = new Date();
                            beginTime.setDate(beginTime.getDate() - 7);
                            beginTime = beginTime.toISOString();
                            $scope.params.startTime = beginTime;
                            $scope.params.endTime = endTime;
                        } else if ($scope.dateRange.rangeType === "ONE_MONTH") {
                            var endTime = (new Date()).toISOString(),
                                beginTime = new Date();
                            beginTime.setMonth(beginTime.getMonth() - 1);
                            beginTime = beginTime.toISOString();
                            $scope.params.startTime = beginTime;
                            $scope.params.endTime = endTime;
                        }
                    }

                    $timeout(function () {
                        var param = {};
                        $dataSourceManager.dataSources["reportSource"].params = param;
                        $dataSourceManager.dataSources["reportSource"].doRequestData();
                    },100);
                }
            ]
        );

    module.filter("setDate", function () {
        return function (data) {
            if(data!=null&&data!="undefined"){
                
            }
        }
    });
    return module;

});
